package com.orangelife.mobile.common.biz;

import android.os.Handler;

import com.common.sp.UserSp;
import com.curry.android.callback.ICallBack;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.constants.Constant;

import java.util.HashMap;
import java.util.Map;

@Deprecated
public class JSONRequest {

    private final String TAG = JSONRequest.class.getSimpleName();
    private JSONRequestListener jsonRequestListener;
    public static final int GET = 0;
    public static final int POST = 1;
    public static final int PUT = 2;
    public static final int DELETE = 3;
    public CurryApplication app = CurryApplication.getInstance();
    private static JSONRequest request;

    public static JSONRequest getInstance() {
        if (request == null) {
            request = new JSONRequest();
        }
        return request;
    }

    private JSONRequest() {

    }

    /**
     * @param obj     数据包
     * @param handler
     * @param action  提交模式
     * @type 提交方式 ，URL，JSON,XML
     */

    public void sendRequest(int action, Object obj, Handler handler, int type) {
        try {
            switch (action) {
                case GET:
                    requestGet(obj, handler, type);
                    break;
                case POST:
                    requestPost(obj, handler, type);
                    break;
                case PUT:
                    requestPut(obj, handler, type);
                    break;
                case DELETE:
                    requestDelete(obj, handler);
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void sendRequest(int action, String url, Object obj, Handler handler, int wat, int type) {
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("url", url);
        rootMap.put("map", obj);
        rootMap.put("wat", wat);
        try {
            switch (action) {
                case GET:
                    requestGet(rootMap, handler, type);
                    break;
                case POST:
                    requestPost(rootMap, handler, type);
                    break;
                case PUT:
                    requestPut(rootMap, handler, type);
                    break;
                case DELETE:
                    requestDelete(rootMap, handler);
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 已废弃，使用单例模式JSONRequest.getInstance().sendRequest(...)替代
     *
     * @param obj     数据包
     * @param handler
     * @param action  提交模式
     * @type 提交方式 ，URL，JSON,XML
     */
    @Deprecated
    public JSONRequest(int action, Object obj, Handler handler, int type) {
        sendRequest(action, obj, handler, type);
    }

    /**
     * @param action
     * @param url
     * @param obj
     * @param jsonRequestListener
     * @param type
     */
    public void sendRequest(int action, String url, Object obj, JSONRequestListener jsonRequestListener, int type) {
        this.jsonRequestListener = jsonRequestListener;
        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("url", url);
        rootMap.put("map", obj);
        try {
            switch (action) {
                case GET:
                    requestGet(rootMap, type);
                    break;
                case POST:
                    requestPost(rootMap, type);
                    break;
                case PUT:
                    requestPut(rootMap, type);
                    break;
                case DELETE:
                    requestDelete(rootMap);
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param obj                 数据包
     * @param action              提交模式
     * @param jsonRequestListener 绑定监听
     * @type 提交方式 ，URL，JSON,XML
     */
    @Deprecated
    public JSONRequest(int action, Object obj,
                       JSONRequestListener jsonRequestListener, int type) {
        this.jsonRequestListener = jsonRequestListener;
        try {
            switch (action) {
                case GET:
                    requestGet(obj, type);
                    break;
                case POST:
                    requestPost(obj, type);
                    break;
                case PUT:
                    requestPut(obj, type);
                    break;
                case DELETE:
                    requestDelete(obj);
                    break;
                default:

                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据提交
     *
     * @param obj
     * @param action
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    private void requestGet(Object obj, int action) throws Exception {
        Map<String, Object> rootMap = (Map<String, Object>) obj;
        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        HttpUtil http = HttpUtil.getHttp();
        http.get(rootMap.get("url").toString(), map, 0, null, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, wat);
            }
        }, action);

    }

    @SuppressWarnings("unchecked")
    private void requestGet(Object obj, Handler handler, int action)
            throws Exception {
        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        int wat = Integer.parseInt(rootMap.get("wat").toString());

        HttpUtil http = HttpUtil.getHttp();

        http.get(rootMap.get("url").toString(), map, wat, handler, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, handler, wat);
            }
        }, action);
    }

    /**
     * 数据提交
     *
     * @param obj
     * @param action
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    private void requestPost(Object obj, int action) throws Exception {

        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        HttpUtil http = HttpUtil.getHttp();

        http.post(rootMap.get("url").toString(), map, 0, null, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, wat);
            }
        }, action);

    }

    @SuppressWarnings("unchecked")
    private void requestPost(Object obj, Handler handler, int action)
            throws Exception {
        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        int wat = Integer.parseInt(rootMap.get("wat").toString());

        HttpUtil http = HttpUtil.getHttp();

        http.post(rootMap.get("url").toString(), map, wat, handler, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, handler, wat);
            }
        }, action);
    }

    /**
     * 数据提交
     *
     * @param obj
     * @param action
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    private void requestPut(Object obj, int action) throws Exception {

        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        HttpUtil http = HttpUtil.getHttp();

        http.put(rootMap.get("url").toString(), map, 0, null, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, wat);
            }
        }, action);

    }

    @SuppressWarnings("unchecked")
    public void requestPut(Object obj, Handler handler, int action)
            throws Exception {
        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        int wat = Integer.parseInt(rootMap.get("wat").toString());

        HttpUtil.getHttp().put(rootMap.get("url").toString(), map, wat, handler,
                new ICallBack() {
                    @Override
                    public void request(String buf, Handler handler, int wat)
                            throws Exception {
                        handlerMessage(buf, handler, wat);
                    }
                }, action);
    }

    @SuppressWarnings("unchecked")
    private void requestDelete(Object obj) throws Exception {

        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        HttpUtil http = HttpUtil.getHttp();

        http.delete(rootMap.get("url").toString(), map, 0, null, new ICallBack() {
            @Override
            public void request(String buf, Handler handler, int wat)
                    throws Exception {
                handlerMessage(buf, wat);
            }
        });

    }

    @SuppressWarnings("unchecked")
    public void requestDelete(Object obj, Handler handler)
            throws Exception {
        Map<String, Object> rootMap = (Map<String, Object>) obj;

        Map<String, Object> map = (Map<String, Object>) rootMap.get("map");

        int wat = Integer.parseInt(rootMap.get("wat").toString());

        HttpUtil.getHttp().delete(rootMap.get("url").toString(), map,
                wat, handler,
                new ICallBack() {
                    @Override
                    public void request(String buf, Handler handler, int wat)
                            throws Exception {
                        handlerMessage(buf, handler, wat);
                    }
                });
    }

    /**
     * 判断如果buf返回500 表示 连接网路失败，2001和1011 表示重新登录
     *
     * @param buf
     */
    private void handlerMessage(String buf, int wat) {
        if (wat != 500) {
            Map<String, Object> map = JSONHelper.jsonToMap(buf);
            int errCode = Integer.parseInt(map.get("errCode") + "");
            String errInfo = map.get("errInfo").toString();
            String context = errCode(errCode, errInfo);
            if (errCode == 2001 || errCode == 1011) {
                jsonRequestListener.request(1011, buf);
            } else if (errCode != 0) {
                jsonRequestListener.request(500, context);
            } else {
                jsonRequestListener.request(errCode, buf);
            }
        } else {
            jsonRequestListener.request(500, Constant.JSON500);
        }
    }

    private final int NETWORK_ERROR = 500;
    /**
     * 网络连接失败
     **/
    private final int LOGIC_ERROR = 201;
    /**
     * 业务逻辑错误
     **/
    private final int LOGIC_ORDERLY = 200;

    /**
     * 判断如果buf返回500 表示 连接网路失败，2001和1011 表示重新登录,业务判断失败为201
     *
     * @param buf
     * @param handler
     * @param wat
     */
    private void handlerMessage(String buf, Handler handler, int wat) {

        if (wat != 500) {
            Map<String, Object> map = JSONHelper.jsonToMap(buf);
            int errCode = Integer.parseInt(map.get("errCode") + "");
            String errInfo = map.get("errInfo").toString();
            String context = errCode(errCode, errInfo);
            if (errCode == 2001 || errCode == 1011) {
                handler.obtainMessage(1011, context).sendToTarget();

            }
            //			else if( errCode == 98 ){
            //				handler.obtainMessage(LOGIC_ERROR, map.get("errInfo")).sendToTarget();
            //			}
            else if (errCode == 65) {
                handler.obtainMessage(65, context).sendToTarget();
            } else if (errCode == 99) {
                handler.obtainMessage(99, context).sendToTarget();
            } else if (errCode == 1001) {
                handler.obtainMessage(1001, context).sendToTarget();
            } else if (errCode == 15001) {
                handler.obtainMessage(15001, context).sendToTarget();
            } else if (errCode == 23001) {
                handler.obtainMessage(23001, context).sendToTarget();
            } else if (errCode == 23002) {
                handler.obtainMessage(23002, context).sendToTarget();
            } else if (errCode == 13004) {
                handler.obtainMessage(13004, context).sendToTarget();
            } else if (errCode == 23016) {
                handler.obtainMessage(23016, context).sendToTarget();
            } else if (errCode == 1003) {
                handler.obtainMessage(1003, context).sendToTarget();
            } else if (errCode == 66002) {
                handler.obtainMessage(66002, context).sendToTarget();
            } else if (errCode != 0) {
                handler.obtainMessage(NETWORK_ERROR, context).sendToTarget();

            } else {
                handler.obtainMessage(wat, buf).sendToTarget();
            }
        } else {
            handler.obtainMessage(NETWORK_ERROR, "网络连接不稳定").sendToTarget();
        }
    }


    private String errCode(int errCode, String errInfo) {
        String str = null;
        switch (errCode) {
            case 0:
                str = app.getResources().getString(R.string.code_0);
                break;
            case 99:
                str = "对不起，服务器开小差了，请稍后再试...";
                break;
            case 6011:
                str = app.getResources().getString(R.string.code_default);
                break;
            case 2001:
                str = app.getResources().getString(R.string.code_1011);
                break;
            case 10023:
                str = app.getResources().getString(R.string.code_10023);
                break;
            case 10055:
                str = "存在的用户昵称";
                break;
            case 10025:
                str = "您已用此手机验证过";
                break;
            case 10026:
                str = "您的新手机号码已经存在！";
                break;
            case 10033:
                str = app.getResources().getString(R.string.code_10033);
                break;
            case 40003:
                str = app.getResources().getString(R.string.code_40003);
                break;
            case 10034:
                str = app.getResources().getString(R.string.code_10034);
                break;
            case 10035:
                str = app.getResources().getString(R.string.code_10035);
                break;
            case 10036:
                str = app.getResources().getString(R.string.code_10036);
                break;
            case 2:
                str = "密码不能为空";
                break;
            case 10001:
                str = app.getResources().getString(R.string.code_10001);
                break;
            case 65:
                str = app.getResources().getString(R.string.code_65);
                break;
            case 1001:
                str = errInfo;
                break;
            case 15001:
                str = app.getResources().getString(R.string.code_15001);
                break;
            case 23001:
                str = app.getResources().getString(R.string.code_23001);
                break;
            case 23002:
                str = app.getResources().getString(R.string.code_23002);
                break;
            case 23016:
                str = app.getResources().getString(R.string.code_23016);
                break;
            case 13004:
                str = app.getResources().getString(R.string.code_13004);
                break;
            case 10032:
                str = app.getResources().getString(R.string.code_10032);
                break;
            case 1003:
                str = app.getResources().getString(R.string.code_1003);
                break;
            case 66002:
                str = app.getResources().getString(R.string.code_66002);
                break;
            default:
                //			str = app.getResources().getString(R.string.code_99);
                str = errInfo;
                break;
        }
        return str;
    }

    public interface JSONRequestListener {
        public void request(int errCode, String buf);
    }

}
